import request from "@/utils/request";
import { AdminInfo } from "./user.api";

const CATEGORY_BASE_URL = "/api/admin";

const CategoryAPI = {
  // 获取分类名称数据
  getCategoryName() {
    return request<any, CategoryName[]>({
      url: `${CATEGORY_BASE_URL}/categoryName`,
      method: "get",
    });
  },

  // 修改数据
  changeCategory(editedData: EditCategory) {
    return request<any, []>({
      url: `${CATEGORY_BASE_URL}/changeCategory`,
      method: "post",
      data: editedData,
    });
  },

  // 获取所编辑的分类的数据
  getEditedCategory(category_id: number) {
    return request<any, EditCategory>({
      url: `${CATEGORY_BASE_URL}/editedCategory`,
      method: "get",
      params: { category_id },
    });
  },

  // 添加分类
  addCategory(
    category_name: string,
    intro: string,
    is_show: boolean,
    add_date: number,
    sort: number,
    admin_id: number
  ) {
    return request<any, {}>({
      url: `${CATEGORY_BASE_URL}/addCategory`,
      method: "post",
      data: { category_name, intro, is_show, add_date, sort, admin_id },
    });
  },

  // 删除选中分类
  delSeleted(category_ids: string) {
    return request<any, {}>({
      url: `${CATEGORY_BASE_URL}/selectedCategory`,
      method: "delete",
      data: { category_ids },
    });
  },

  // 删除分类
  delCategory(category_id: number) {
    return request<any, AdminInfo[]>({
      url: `${CATEGORY_BASE_URL}/category`,
      method: "delete",
      data: { category_id },
    });
  },

  // 获取分类数据
  getCategory(page: number, pagesize: number, searchData?: SearchData) {
    return request<any, CategoryCount>({
      url: `${CATEGORY_BASE_URL}/category`,
      method: "get",
      params: { page, pagesize, ...searchData },
    });
  },
};

export default CategoryAPI;

export interface CategoryName {
  category_id: number;
  category_name: string;
}

// 分类和分类数量类型
export interface CategoryCount {
  result: Category[];
  count: number;
}

// 分类数据类型
export interface Category {
  category_id: number;
  category_name: string;
  nickname: string;
  sort: number;
  is_show: number;
  add_date: number;
  intro: string;
  admin_id: number;
}

// 编辑的分类数据类型
export interface EditCategory {
  category_name: string;
  intro: string;
  is_show: boolean;
  add_date: number;
  sort: number;
  admin_id: number;
}

// 搜索的数据类型
export interface SearchData {
  category_name: string;
  is_show: number | string;
  startDate: string;
  endDate: string;
}
